package tech.aistar.day01;

import tech.aistar.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * 本类用来演示: 转发和重定向
 *
 * @author: success
 * @date: 2021/4/15 1:38 下午
 */
@WebServlet(urlPatterns = "/day02/forward/from")//模糊匹配<精确匹配
public class ForwardDemo extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out = resp.getWriter();

        out.println("from...");//不会在页面中进行展示

        //未来 - 就是在调用service层

        List<User> users = new ArrayList<>();
        User u1 = new User(1,"admin");
        User u2 = new User(2,"tom");
        User u3 = new User(3,"jack");
        users.add(u1);
        users.add(u2);
        users.add(u3);


        req.setAttribute("users",users);

        //request作用域 - 一次请求,一次响应
        req.setAttribute("username","tom");

        //跳转的问题
        //转发 - servlet程序
        //req.getRequestDispatcher("/day02/forward/to").forward(req,resp);

        //转发 - jsp程序
        req.getRequestDispatcher("/user/hello.jsp").forward(req,resp);


        //req.getRequestDispatcher("/WEB-INF/hello.jsp").forward(req,resp);

        //转发包含...
        //req.getRequestDispatcher("/day02/forward/to").include(req,resp);

        //重定向
        //本质 - 是将这个新的地址响应给了客户端
        //resp.sendRedirect("/aistar/day02/forward/to");
    }
}
